package com.wangcang.widgetlibrary.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.bean.CityBean;

import java.util.List;

/**
 * @author : mundane
 * @time : 2017/7/11 20:10
 * @description :
 * @file : TitleItemDecoration.java
 */

public class TitleItemDecoration extends RecyclerView.ItemDecoration {
	private int mTagHeight;
	private Paint mTagPaint;
	private int mBlueColor;
	private List<CityBean> mData;
	private Paint mTextPaint;
	private int mFontSize;
	private int mTextMarginLeft;
	private int mDividerHeight;

	public TitleItemDecoration() {
	}

	public TitleItemDecoration(Context context, List<CityBean> data) {
		mTagHeight = context.getResources().getDimensionPixelSize(R.dimen.common_dimen_dp40);
		mDividerHeight = context.getResources().getDimensionPixelSize(R.dimen.common_dimen_dp1);
		mTextMarginLeft = context.getResources().getDimensionPixelSize(R.dimen.common_dimen_dp20);
		mData = data;
		int color = Color.parseColor("#FFDFDFDF");
		mTagPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		mTextPaint.setColor(Color.BLACK);
		mFontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, context.getResources().getDisplayMetrics());
		mTextPaint.setTextSize(mFontSize);
		mTagPaint.setColor(color);
	}

	@Override
	public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
		super.getItemOffsets(outRect, view, parent, state);
		int position = parent.getChildAdapterPosition(view);
		String tag = mData.get(position).getTag();
		//	如果是组中的第一个位置, 为悬浮栏预留出位置
		if (isFirstInGroup(position, tag)) {
			outRect.top = mTagHeight;
		} else {
			outRect.top = mDividerHeight;
		}
	}

	//	判断是不是组中的第一个位置
	//	根据前一个组名，判断当前是否为新的组
	private boolean isFirstInGroup(int pos, String tag) {
		if (pos == 0) {
			return true;
		} else {
			String prevTag = mData.get(pos - 1).getTag();
			return !TextUtils.equals(prevTag, tag);
		}
	}

	@Override
	public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
		super.onDraw(c, parent, state);
	}

	@Override
	public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
		super.onDrawOver(c, parent, state);
		int itemCount = state.getItemCount();
		int childCount = parent.getChildCount();
		int left = parent.getPaddingLeft();
		int right = parent.getWidth() - parent.getPaddingRight();
		String prevTag; // 标记上一个item对应的tag
		String currentTag = null; // 当前item对应的tag
		for (int i = 0; i < childCount; i++) {
			View child = parent.getChildAt(i);
			int position = parent.getChildAdapterPosition(child);
			prevTag = currentTag;
			currentTag = mData.get(position).getTag();
			if (TextUtils.equals(currentTag, prevTag)) { //	如果该item的tag和上一个tag相同则只画分割线
				if (child.getTop() < mTagHeight) {
					// 高度小于顶部悬浮栏时，跳过绘制
					continue;
				}
				c.drawRect(left + mTextMarginLeft, child.getTop() - mDividerHeight, right, child.getTop(), mTagPaint);
			} else { //	如果该item的tag和上一个tag不同, 在该item的头部画文字
				//	下一个tag的top坐标
				int nextTagTop = child.getBottom();
				//	top将在mGroupHeight和view.getTop()中取最大值，也就是说top将不会小于mGroupHeight，这样就能实现吸顶效果
				int bottom = Math.max(mTagHeight, child.getTop());
				if (position + 1 < itemCount) {
					//	获取下个tag
					String nextTag = mData.get(position + 1).getTag();
					// 下一组的第一个tag贴近当前的tag(nextTagTop <= bottom), 当前的tag的bottom才随之减小, bottom取当前childView的getBottom, 产生挤压上个tag并且把上一个tag顶上去的效果
					if (!TextUtils.equals(currentTag, nextTag) ) {
						bottom = Math.min(nextTagTop, bottom);
					}
				}
				// 根据top绘制tag
				c.drawRect(left, bottom - mTagHeight, right, bottom, mTagPaint);
				Paint.FontMetrics metrics = mTextPaint.getFontMetrics();
				//文字竖直居中显示
				float baseLine = bottom - (mTagHeight - (metrics.bottom - metrics.top)) / 2 - metrics.bottom;
				c.drawText(currentTag, left + mTextMarginLeft, baseLine, mTextPaint);
			}
		}
	}
}
